<?php
/**
 * Created by PhpStorm.
 * User: clb
 * Date: 2019/1/21
 * Time: 15:02
 */

namespace app\server\model;

use app\server\inter\UploadStrategy;

class Upload
{

    protected $_strategy;

    public $errorMsg = null;

    /**
     * 开始上传
     *
     * @return array
     */
    public function run(): array
    {
        return $this->_strategy->upload();
    }

    /**
     * 注入上传文件策略
     *
     * @param UploadStrategy $strategy
     */
    public function setStrategy(UploadStrategy $strategy)
    {
        $this->_strategy = $strategy;
    }

    /**
     * 检查文件大小
     */
    public function checkSize()
    {
        foreach ($_FILES as $FILE) {
            if ($FILE['file_size'] > Config::get('file.upload_max_size')) {
                $this->errorMsg =  '大文件请切片上传';
            }
        }
    }

    /**
     * 文件是否为空
     */
    public function isEmpty()
    {
        if (empty($_FILES)) {
            $this->errorMsg = '请上传文件';
        }
    }
}